﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace AUV5.Web.Service
{
    public partial class UserChangePassword : PageBaseService
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string userid = Request["userid"]??"";
            string psw = Request["password"]??"";
            string newpsw = Request["newpassword"]??"";
            string secretkey = Request["secretkey"] ?? "";

            if (!IsValidKey(secretkey, userid, psw, newpsw))
            {
                Response.Write("112");//密钥不正确 
                return;
            }


            if (Request["userid"] != null && Request["password"] != null && Request["newpassword"] != null)
            {
                Model.UserInfo user = BLL.UserInfoBLL.Instance.GetDetail(userid);
                if (user != null)
                {
                    if (user.Password == psw)
                    {
                        user.Password = newpsw;
                        user.UpdateDate = DateTime.Now;

                        BLL.UserInfoBLL.Instance.Update(user);
                        Response.Write("100");//修改成功
                    }
                    else
                    {
                        Response.Write("304");//旧密码不正确
                    }
                }
                else
                {
                    Response.Write("301");
                }   
            }
            else
            {
                Response.Write("111");//参数不正确 
            }
        }

        private bool IsValidKey(string secretkey, string userid, string psw, string newpsw)
        {
            if (secretkey_enable)
            {
                if (string.IsNullOrEmpty(secretkey))
                {
                    return false;
                }
                else
                {
                    string finalkey = base.EncryptMD5(public_key + current_day + userid + psw + newpsw).Substring(6, 22);
                    return finalkey == secretkey;
                }
            }
            else
            {
                return true;
            }
        }
    }
}